package com.wingsoft.work.service.impl;

import com.wingsoft.common.exception.ServiceException;
import java.util.ArrayList;
import java.util.List;

import com.wingsoft.work.domain.UserTask;
import org.apache.commons.lang3.ObjectUtils;
import com.wingsoft.common.utils.bean.BeanUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.wingsoft.work.mapper.UserTaskLogMapper;
import com.wingsoft.work.domain.UserTaskLog;
import com.wingsoft.work.service.IUserTaskLogService;
import com.wingsoft.common.utils.IdGenerator.IdGeneratorFactory;
import com.wingsoft.work.dto.UserTaskLogDto;
import com.wingsoft.work.vo.UserTaskLogVo;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 用户任务日志Service业务层处理
 *
 * @author hemj
 * @date 2025-10-20
 */
@Service
public class UserTaskLogServiceImpl extends MPJBaseServiceImpl<UserTaskLogMapper, UserTaskLog> implements IUserTaskLogService
{
    @Autowired
    private IdGeneratorFactory idGeneratorFactory;

    /**
         * 验证参数
         */
    private void validateData(UserTaskLog userTaskLog){
        if (ObjectUtils.isEmpty(userTaskLog.getUserId())){
            throw new ServiceException("用户id不能为空");
        }
        if (ObjectUtils.isEmpty(userTaskLog.getTaskId())){
            throw new ServiceException("任务id不能为空");
        }
        if (ObjectUtils.isEmpty(userTaskLog.getDes())){
            throw new ServiceException("描述不能为空");
        }
    }

    /**
     * 保存数据
     */
    @Transactional(timeout = 3000, isolation = Isolation.READ_UNCOMMITTED, propagation = Propagation.REQUIRED)
    public UserTaskLog saveCascade(UserTaskLogDto userTaskLogDto){
        UserTaskLog userTaskLog = new UserTaskLog();
        BeanUtils.copyProperties(userTaskLogDto,userTaskLog);
        validateData(userTaskLog);
        userTaskLog.setId(idGeneratorFactory.getId(UserTaskLog.class.getSimpleName()));
        this.save(userTaskLog);
        return userTaskLog;
    }
    /**
     * 编辑数据
     */
    @Transactional(timeout = 3000, isolation = Isolation.READ_UNCOMMITTED, propagation = Propagation.REQUIRED)
    public UserTaskLog updateCascade(UserTaskLogDto userTaskLogDto){
        UserTaskLog userTaskLog = this.getById(userTaskLogDto.getId());;
        BeanUtils.copyPropertiesIgnoreNull(userTaskLogDto,userTaskLog);
        validateData(userTaskLog);
        this.updateById(userTaskLog);
        return userTaskLog;
    }

    /**
     * 初始化查询条件
     */
    public MPJLambdaWrapper<UserTaskLog> initQueryWrapper(UserTaskLogDto userTaskLogDto){
        MPJLambdaWrapper<UserTaskLog> queryWrapper = new MPJLambdaWrapper<>();
        if (!ObjectUtils.isEmpty(userTaskLogDto.getUserId())) {
            queryWrapper.eq(UserTaskLog::getUserId, userTaskLogDto.getUserId());
        }
        if (!ObjectUtils.isEmpty(userTaskLogDto.getTaskId())) {
            queryWrapper.eq(UserTaskLog::getTaskId, userTaskLogDto.getTaskId());
        }
        if (!ObjectUtils.isEmpty(userTaskLogDto.getDes())) {
            queryWrapper.eq(UserTaskLog::getDes, userTaskLogDto.getDes());
        }
        queryWrapper.orderByDesc(UserTaskLog::getId);
        return queryWrapper;
    }

    /**
     * list查询
     */
    public List<UserTaskLogVo> queryList(UserTaskLogDto userTaskLogDto) {
        List<UserTaskLog> userTaskLogs = this.selectJoinList(UserTaskLog.class,initQueryWrapper(userTaskLogDto));
        List<UserTaskLogVo> vos = new ArrayList<>();
        for (UserTaskLog userTaskLog:userTaskLogs) {
            UserTaskLogVo userTaskLogVo = new UserTaskLogVo();
            BeanUtils.copyProperties(userTaskLog,userTaskLogVo);
            vos.add(userTaskLogVo);
        }
        return vos;
    }

    /**
     * 统计
     */
    public long countList(UserTaskLogDto userTaskLogDto) {
        return this.count(initQueryWrapper(userTaskLogDto));
    }

    /**
     * 获取单个数据
     */
    public UserTaskLogVo getOneById(Long id){
        UserTaskLog userTaskLog = this.getById(id);
        UserTaskLogVo userTaskLogVo = new UserTaskLogVo();
        BeanUtils.copyProperties(userTaskLog,userTaskLogVo);
        return userTaskLogVo;
    }

    /**
     * 删除数据
     * @param ids
     */
    @Transactional(timeout = 3000, isolation = Isolation.READ_UNCOMMITTED, propagation = Propagation.REQUIRED)
    public void removeByIdList(List<Long> ids) {
        this.removeByIds(ids);
    }

    public void saveByTask(UserTask userTask, String des,String result) {
        UserTaskLog userTaskLog = new UserTaskLog();
        userTaskLog.setUserId(userTask.getUserId());
        userTaskLog.setDes(des);
        userTaskLog.setTaskId(userTask.getId());
        userTaskLog.setId(idGeneratorFactory.getId(UserTaskLog.class));
        userTaskLog.setApijson(result);
        this.save(userTaskLog);
    }
}
